package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.domain.CourseMarket;
import cn.itsource.ymcc.domain.CourseUserLearn;
import cn.itsource.ymcc.dto.PayAliNotifyDto;
import cn.itsource.ymcc.mapper.CourseUserLearnMapper;
import cn.itsource.ymcc.service.ICourseMarketService;
import cn.itsource.ymcc.service.ICourseUserLearnService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Cherish
 * @since 2022-08-07
 */
@Service
public class CourseUserLearnServiceImpl extends ServiceImpl<CourseUserLearnMapper, CourseUserLearn> implements ICourseUserLearnService {
    @Autowired
    private ICourseMarketService courseMarketService;

    @Override
    public CourseUserLearn checkUserLearn(Long loginId, Long courseId) {
        Wrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("login_id",loginId);
        wrapper.eq("course_id",courseId);
        return selectOne(wrapper);
    }

    @Override
    public void addUserLearn(PayAliNotifyDto dto) {
        //* 1.根据 扩展参数得到loginId + courseId
        Map<String,Object> map = JSONObject.parseObject(dto.getExtParams(), Map.class);
        //* 2.根据loginId和courseId查询 购买记录
        Long loginId = Long.valueOf(map.get("loginId").toString());
        String courseIdStr = map.get("courseId").toString();//可能是多个
        String[] ids = courseIdStr.split(",");
        Date now = new Date();
        for (String id : ids) {
            Long courseId = Long.valueOf(id);

            CourseUserLearn tmp = selectByLoginIdAndCourseId(loginId, courseId);
            CourseMarket courseMarket = courseMarketService.selectById(courseId);
            //* 3.查询不到 直接新增
            if(tmp == null){
                CourseUserLearn learn = new CourseUserLearn();
                learn.setLoginId(loginId);
                learn.setStartTime(now);
                learn.setEndTime(DateUtils.addDays(now,courseMarket.getValidDays()));
                learn.setState(0);
                learn.setCourseId(courseId);
                learn.setCourseOrderNo(dto.getOrderNo());
                learn.setCreateTime(now);
                insert(learn);
            }else{
                //* 4.查询到了
                //*  4.1 增加可光看时间  + 覆盖orderNo
                tmp.setCourseOrderNo(dto.getOrderNo());
                tmp.setEndTime(DateUtils.addDays(tmp.getEndTime(),courseMarket.getValidDays()));
                updateById(tmp);
            }
        }
    }


    public CourseUserLearn selectByLoginIdAndCourseId(Long loginId, Long courseId){
        Wrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("login_id",loginId);
        wrapper.eq("course_id",courseId);
        return selectOne(wrapper);
    }
}
